﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Sobiens.Connector.Admin.BLL
{
    //TODO: refactor this, is duplicated
    [DataContract(Namespace = "", Name = "Configurations")]
    public class AppConfigurations
    {
        private static DataContractSerializer _dcs = new DataContractSerializer(typeof(AppConfigurations));
        private static DataContractJsonSerializer _dcjs = new DataContractJsonSerializer(typeof(AppConfigurations));

        [DataMember(Order = 1)]
        public AppConfiguration[] Configurations { get; set; }

        public String GetJSON()
        {
            return Serialize(_dcjs);
        }

        public static AppConfigurations CreateFromJSON(String json)
        {
            return Deserialize(_dcjs, json);
        }

        public void ProcessNewElements()
        {
            //creeaza cate un item pt fiecare item nou
            
            //foreach (SiteSetting siteSetting in SiteSettings)
            //{
            //    if (siteSetting.ID == null)
            //        siteSetting.ID = Guid.NewGuid().ToString();
            //}
        }

        #region private methods

        private static AppConfigurations Deserialize(XmlObjectSerializer serializer, String data)
        {
            AppConfigurations appConfig = serializer.ReadObject(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(data))) as AppConfigurations;
            return appConfig;
        }

        private String Serialize(XmlObjectSerializer serializer)
        {
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, this);
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        #endregion
    }
}
